package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 *购物车接口实现类
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //1.判断购物车中是否存在该菜品或套餐
        //1.1.根据用户id和菜品或套餐id查询购物车数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        //2.如果存在，数量+1
       if (list != null && list.size() > 0){
           ShoppingCart cart = list.get(0);
           cart.setNumber(cart.getNumber()+1);
           shoppingCartMapper.updateNumberById(cart);//sql: update shopping_cart set number = number + 1 where id = ?
       }else {
           //3.如果不存在，添加到购物车，数量默认为1
           //3.1.判断添加的是菜品还是套餐
           if (shoppingCartDTO.getDishId() != null){

               //添加的是菜品=
               Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
               shoppingCart.setAmount(dish.getPrice());
               shoppingCart.setName(dish.getName());
               shoppingCart.setImage(dish.getImage());

           }else {

               //添加的是套餐
               Setmeal setmeal = setmealMapper.getByIdWithDish(shoppingCartDTO.getSetmealId());
               shoppingCart.setAmount(setmeal.getPrice());
               shoppingCart.setName(setmeal.getName());
               shoppingCart.setImage(setmeal.getImage());
           }
           shoppingCart.setNumber(1);
           shoppingCart.setCreateTime(LocalDateTime.now());
           //插入数据
           shoppingCartMapper.insert(shoppingCart);
       }

    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        //1.获取当前用户id
        Long userId = BaseContext.getCurrentId();
        //2.查询
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(userId)
                .build();
        List<ShoppingCart> List = shoppingCartMapper.list(shoppingCart);
        return List;
    }

    /**
     * 删除购物车一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {

        //1.创建条件对象,获取用户ID
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        //2.根据用户id和菜品或套餐id删除购物车单条数据
        //2.1.查询购物车数据
        ShoppingCart cart = shoppingCartMapper.list(shoppingCart).get(0);

        if (cart.getNumber() > 1){
            //一次只能删除一个商品,number > 1 ,数量-1
            cart.setNumber(cart.getNumber() - 1);
            shoppingCartMapper.updateNumberById(cart);

        }else {
            //number = 1 ,删除购物车数据
            shoppingCartMapper.deleteByID(shoppingCart);
        }
    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCart() {

        //sql: delete from shopping_cart where user_id = ?
        shoppingCartMapper.deleteByID(ShoppingCart.builder().userId(BaseContext.getCurrentId()).build());
    }
}
